package CelestialWeb;

import CelestialDataAccess.Customer;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import CelestialWorkflow.*;
import CelestialDataAccess.*;

public class ShoppingCartServlet1 extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String strType = request.getParameter("strType");
        int customerID;

        response.setContentType("text/xml");
        response.setHeader("Cache-control", "no-cache");
        PrintWriter pw = response.getWriter();
        pw.println("<Details>");

        ShoppingCartControl scctrl = new ShoppingCartControl();
        if (strType != null) {
            if (strType.equals("check")) {
                AccountControl m_AccountControl = new AccountControl();
                String cc_num = request.getParameter("cc_num");
                int type = Integer.parseInt(request.getParameter("cc_type"));
                if (!m_AccountControl.validateCreditCardNumber(cc_num)) {
                    pw.println("<Error>Error!</Error>");
                }
                if (!m_AccountControl.validateCreditCardIssuer(cc_num, type)) {
                    pw.println("<Error>Error!</Error>");
                }

            } else if (strType.equals("suggest")) {
                customerID = Integer.parseInt(request.getParameter("customerID"));
                String strProducts = request.getParameter("strProducts");
                List<Product> productList = new ArrayList<Product>();
                scctrl.suggestProducts(strProducts, customerID, productList);
                for (Product curProduct : productList) {
                    pw.write("<Product>");
                    pw.write("<ProductID>");
                    pw.write(curProduct.getProductID());
                    pw.write("</ProductID>");
                    pw.write("<ProductName>");
                    pw.write(curProduct.getProductName());
                    pw.write("</ProductName>");
                    pw.write("<ProductImage>");
                    pw.write(curProduct.getImage1());
                    pw.write("</ProductImage>");
                    pw.write("<ProductPrice>");
                    pw.write("" + curProduct.getPrice());
                    pw.write("</ProductPrice>");
                    pw.write("</Product>");
                }

            } else if (strType.equals("fill")) {
                customerID = Integer.parseInt(request.getParameter("customerID"));
                Customer usr = scctrl.getCustomerById(customerID);
                if (usr != null) {
                    pw.println("<LastName>" + usr.getLastName() + "</LastName>");
                    pw.println("<Email>" + usr.getEmail() + "</Email>");
                    pw.println("<CCNo>" + usr.getCreditCardNumber() + "</CCNo>");
                    pw.println("<CardType>" + usr.getCreditCardType() + "</CardType>");
                    pw.println("<CardHolder>" + usr.getCreditCardHolder() + "</CardHolder>");
                    pw.println("<Country>" + usr.getCountry() + "</Country>");
                    pw.println("<City>" + usr.getCity() + "</City>");
                    pw.println("<State>" + usr.getState() + "</State>");
                    pw.println("<AddressLine1>" + usr.getAddressLine1() + "</AddressLine1>");
                    pw.println("<AddressLine2>" + usr.getAddressLine2() + "</AddressLine2>");
                    pw.println("<PostalCode>" + usr.getPostalCode() + "</PostalCode>");
                }
            } else {
                customerID = Integer.parseInt(request.getParameter("customerID"));
                String strProducts = request.getParameter("strProducts");
                String strCustomerData = request.getParameter("strProducts");
                String result = scctrl.newOrder(strProducts, strCustomerData, customerID);
                if (!result.equals("-1")) {
                    pw.println("<OrderID>" + result + "</OrderID>");
                }
            }
        }
        pw.println("<ErrorCode>" + scctrl.getError() + "</ErrorCode>");
        pw.println("</Details>");
        pw.close();
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
